package com.sq.partner.manager.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.mybatis.model.TransactionMonitoring;
import com.sq.partner.manager.service.IGatewayClearCacheManager;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author duguangwei
 * 接口监控--> 网关监控
 */
@Controller
@RequestMapping(value = "/monitorManager")
public class MonitorManagerController {
   private static Logger logger = LogUtil.getLog(MonitorManagerController.class);
   
	@Resource
	IGatewayClearCacheManager gatewayClearCacheManager;
	
	@RequestMapping(value = "/monitoring")
	public String monitorManager(ModelMap model,HttpServletRequest request) throws Exception {
		
		return "transManager/monitorManager";
	}
	@RequestMapping("/cacheInfoList")
	public @ResponseBody Page<TransactionMonitoring> cacheInfoList(String monitorServiceIndex,String serviceIp){
		Page<TransactionMonitoring>  page=new Page<TransactionMonitoring>();
		try{
				page=gatewayClearCacheManager.cacheInfoList(monitorServiceIndex,serviceIp);
		}catch(Exception e) {
			page.setResult("error");
			page.setMessage("系统出现异常，请与管理员联系！");
			logger.error("系统出现异常",e);
		}
		return page;
	}
	@RequestMapping("/queryAppname")
	public @ResponseBody Page<TransactionMonitoring> queryAppName(){
		Page<TransactionMonitoring> page= gatewayClearCacheManager.queryAppname();
		return page;
	}
	
	@RequestMapping("/queryServiceIp")
	public @ResponseBody Page<TransactionMonitoring> queryServiceIp(String appId){
		Page<TransactionMonitoring> page= gatewayClearCacheManager.getServiceIp(appId);
		return page;
	}
	@RequestMapping("/clearCacheInfoManager")
	public @ResponseBody Page<TransactionMonitoring> clearCacheInfoManager(Integer monitorServiceIndex,String serviceIp,Integer cacheindex){
 
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		//判断空
		if(null==monitorServiceIndex ||null==cacheindex){
			page.setMessage("操作错误");
			page.setResult("error");
			return page;
		}
		try {
			gatewayClearCacheManager.clearCacheInfoManager(monitorServiceIndex,serviceIp,cacheindex);
		} catch (Exception e) {
			page.setResult("error");
			page.setMessage("清除缓存失败");
			logger.info("清除缓存失败",e);
			return page;
		}
		logger.info("清除缓存成功");
		page.setMessage("清除缓存成功");
		return page;
	}
	
	
	@RequestMapping("/connStatusList")
	public @ResponseBody Page<TransactionMonitoring> connStatusList(Integer monitorServiceIndex,String serviceIp) throws Exception{
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		page=gatewayClearCacheManager.connStatusList(monitorServiceIndex,serviceIp);
		return page;
	}
	@RequestMapping("/operatConn")
	public @ResponseBody Page<TransactionMonitoring> operatConn(Integer monitorServiceIndex,Integer connindex,String serviceIp,String connstatus) throws Exception{
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		if(StrUtil.isEmpty(serviceIp)||StrUtil.isEmpty(connstatus)||null==monitorServiceIndex||null==connindex){
			page.setMessage("操作错误");
			page.setResult("error");
			return page;
		}
		gatewayClearCacheManager.operatConn(monitorServiceIndex,serviceIp,connindex,connstatus);
		page.setMessage("修改成功");
		return page;
	}
	@RequestMapping("/logManagerList")
	public @ResponseBody Page<TransactionMonitoring> logManagerList(Integer monitorServiceIndex,String serviceIp) throws Exception{
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		page=gatewayClearCacheManager.logManagerList(monitorServiceIndex,serviceIp);
		return page;
	}
	
	@RequestMapping("/updateLogLeven")
	public @ResponseBody Page<TransactionMonitoring> updateLogLeven(Integer monitorServiceIndex,String serviceIp,String logName,String logLeven) throws Exception{
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		gatewayClearCacheManager.updateLogLeven(monitorServiceIndex,serviceIp,logName,logLeven);
		page.setMessage("修改成功");
		return page;
	}
	
	@RequestMapping("/resetLog")
	public @ResponseBody Page<TransactionMonitoring> resetLog(Integer monitorServiceIndex,String serviceIp) throws Exception{
		Page<TransactionMonitoring> page=new Page<TransactionMonitoring>();
		gatewayClearCacheManager.resetLog(monitorServiceIndex,serviceIp);
		page.setMessage("修改成功");
		return page;
	}
	
}

